#ifndef GENERAL_H
#define GENERAL_H

#include <stdint.h>
#include "mcu.h"

#define PROJECT_Fender_Left               1
#define PROJECT_Fender_Right              2
#define PROJECT_trunk                     3

#define CURRENT_PROJECT                 PROJECT_Fender_Right

#define PWM_Channel_PWM1				0
#define PWM_Channel_PWM2				1
#define PWM_Channels					2
#define PWM_Val_times					5
#define PWM_Count_MAX					20

#define PWM_Val_Counter_Out				5


#define ALL_LED_NUM     8
#define LED_PL_1        1
#define LED_PL_2        0
#define LED_PL_3        7
#define LED_PL_4        6
#define LED_PL_5        5
#define LED_PL_6        4
#define LED_PL_7        2
#define LED_PL_8        3

#define LIN_CMD_INIT_VALUE  {0,0,0,0,0}

typedef struct
{
    uint8_t  PO_Left_ADC_Status              :1;
    uint8_t  PO_Right_ADC_Status             :1;
    uint8_t  DI_Left_ADC_Status              :1;
	uint8_t  DI_Right_ADC_Status             :1;
    uint8_t  PO_Left_Status              	 :1;
	uint8_t  PO_Right_Status              	 :1;
    uint8_t  DI_Left_Status              	 :1;
    uint8_t  DI_Right_Status              	 :1;

    uint8_t	PO_Left_ADC_Count_H		    :4;
    uint8_t	PO_Right_ADC_Count_H		:4;
    uint8_t	DI_Left_ADC_Count_H		    :4;
    uint8_t	DI_Right_ADC_Count_H		:4;

	uint8_t Swipe_Turn_Signal			:8;

#if	1
    uint8_t Not_Bright_Animation_Status :8;
#endif
	

}LAMB_Statusstruct;

typedef struct
{
    uint8_t  PO_ADC_Status              :1;
    uint8_t  DI_ADC_Status              :1;
	uint8_t  ST_ADC_Status              :1;
    uint8_t  PO_Status              	:1;
    uint8_t  DI_Status              	:1;
    uint8_t  ST_Status              	:1;
	uint8_t  Blank						:2;

    uint8_t	PO_ADC_Count_H		    	:4;
    uint8_t	DI_ADC_Count_H		    	:4;
    uint8_t	ST_ADC_Count_H				:4;
	uint8_t Swipe_Turn_Signal			:2;
    uint8_t Not_Bright_Animation_Status :2;

}LAMB_Fender_Statusstruct;

typedef enum
{
	Off_State= 0,
	On_State,
	LS_State,
	Lamb_GROUP_MAX,
} Lamb_Status_Group;

#define LIN_CMD_INIT_VALUE  {0,0,0,0,0}


#define LED_BRIGHTNESS_MIN        	9

#define LAMP_MODE_INIT              0
#define LAMP_MODE_NORMAL            10
#define LAMP_MODE_ANM1              1
#define LAMP_MODE_ANM2              2
#define LAMP_MODE_ANM3              3
#define LAMP_MODE_GBY1              4
#define LAMP_MODE_GBY2              5
#define LAMP_MODE_GBY3              6
#define LAMP_MODE_LS1               7
#define LAMP_MODE_LS2               8
#define LAMP_MODE_LS3               9
#define LAMP_MODE_LS4               14
#define LAMP_MODE_LS5               15
#define LAMP_MODE_LS6               16
#define LAMP_MODE_LS7               17
#define LAMP_MODE_LS8               18
#define LAMP_MODE_Not_Bright        19
#define LAMP_MODE_OFF               11
#define LAMP_MODE_TxReq             12
#define LAMP_MODE_AcReq             13

#define LAMP_MODE_MS_Animation      18

#define LAMP_MODE_LS27              27
#define LAMP_MODE_LS28              28
#define LAMP_MODE_LS29              29

#define SECTOR_SIZE 0x2000

#define MusicShowDownLoadStartAdress  0x01000000

#define E52249_DEVICE_MIN   1
#define E52249_DEVICE_MAX   4
#define E52249_DEVICE_NUM   (E52249_DEVICE_MAX)

typedef enum
{
	PWM_DIS= 0,
	PWM_EN,
	PWM_NORMAL,
} PWM_Received_Status;

typedef struct {
	uint8_t CcvVal_Counter;
	uint8_t OriginalCounterFinish_Flag;
	int16_t RcvVal_Rising[PWM_Val_times];
	int16_t RcvVal_Falling[PWM_Val_times];
	int16_t CcvVal_Rising[PWM_Val_times];
	int16_t CcvVal_Falling[PWM_Val_times];
	int16_t RtestVal[PWM_Val_times];
	int16_t CtestVal[PWM_Val_times];
} PWM_Capture_OVstruct;

typedef struct {
	PWM_Capture_OVstruct PWM_OV[PWM_Channels];
	uint8_t State;
	uint8_t mode;
	uint8_t pwm_counter[PWM_Count_MAX];
	uint8_t Clear_CcvVal_Counter_R;
	uint8_t Clear_CcvVal_Counter_C;
	int16_t BubbleVal1[PWM_Val_times];
} PWM_Capture_HVstruct;


typedef struct {
	uint8_t PWM_SAVE_FLAG;
	uint8_t save_time_1500ms;
	uint8_t PWM_SAVE_RAM[200];
	uint8_t TEST[200];
	uint8_t Download_Count;
}PWM_DownLoad_struct;

typedef struct {
	PWM_DownLoad_struct DL;
	uint8_t PWM_LS_Flag;
	uint8_t PWM_LS_SW;
	uint8_t PWM_LS_Counter;
	uint8_t PWM_LS_Sts;

} PWM_LightShow_struct;

typedef struct
{
	uint8_t TailLampCtrl;
	uint8_t SideMarkerCtrl;
	uint8_t AnimationCtrl;
	uint8_t TailgateStatus;
	uint8_t ChargeState;
}LinCMD_t;

extern LinCMD_t g_stLinReqCmd;
extern uint8_t g_u8CurWorkMode;
extern volatile PWM_Capture_HVstruct    PWM_Capture_HV;
extern volatile PWM_LightShow_struct		 PWM_LightShow;
extern volatile LAMB_Statusstruct 		 LAMB_Status;
extern volatile LAMB_Fender_Statusstruct   LAMB_Fender_Status;
extern uint16_t g_16SetChannel[16];
extern uint16_t g_16FenderSetChannel[3];
extern uint16_t g_16FenderSetPwmDuty[3][12];
extern uint16_t g_16SetPwmDuty[16][12];
extern uint8_t g_u8LS_Start_Wait;


extern status_t PWM_Register_Value_Handle_Period(volatile PWM_Capture_HVstruct *PWM_Capture_P,uint8_t Times);

extern void Clear_PWM_InputCapture_OV_C(void);

extern void ADC_Update(void);

extern void Light_Update(void);

extern void HALL_50ms(void);

extern void LED_SetDuty(uint8_t u8dev, uint8_t u8chn, uint16_t u16duty_chn);

extern void PWM_Mode_Handler_Special(void);

extern void PWM_Mode_Handler_LS(void);

extern void Task_LightLogic_Handler();


#endif
